<html>
<head><meta charset="utf-8"><title>Synthesizing a new Session for tests · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html">Synthesizing a new Session for tests</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216326565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216326565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216326565">(Nov 11 2020 at 10:05)</a>:</h4>
<p>Hi all! I'm guessing this will end in "you can't do that", but, I figure I'll try asking anyway. So, in rust-gpu (a -Zcodegen-backend for spir-v), we made a "linker" for spir-v modules, and there's tests testing it. There are a surprising number of errors that can happen during linking (due to spir-v requiring a lot of whole-program-analysis passes due to the nature of GPUs), and so we pass in a rustc <code>Session</code> to the linker to report errors. However, for tests, it's rather difficult to synthesize up a dummy Session to pass in - so right now, we take a <code>Option&lt;&amp;Session&gt;</code>, and then <code>if let Some(sess) = sess { report error } else { panic for tests }</code>. That's really annoying, so, is there a way to synthesize a Session for tests so we don't have to do that dance?<br>
<a href="https://github.com/EmbarkStudios/rust-gpu/blob/9c8cec36396a1f328c8582da488d469963982590/crates/rustc_codegen_spirv/src/linker/mod.rs#L89-L91">https://github.com/EmbarkStudios/rust-gpu/blob/9c8cec36396a1f328c8582da488d469963982590/crates/rustc_codegen_spirv/src/linker/mod.rs#L89-L91</a></p>



<a name="216326786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216326786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216326786">(Nov 11 2020 at 10:08)</a>:</h4>
<p>You can use <a href="https://github.com/rust-lang/rust/blob/38030ffb4e735b26260848b744c0910a5641e1db/compiler/rustc_interface/src/util.rs#L61"><code>rustc_interface::utils::create_session</code></a>.</p>



<a name="216326983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216326983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216326983">(Nov 11 2020 at 10:10)</a>:</h4>
<p>Ah neat! Although <code>config::Options</code> has quite a few things to fill out... I guess I should probably just go through the pain of writing the hundred lines of code or whatever to synthesize that, though.</p>



<a name="216327155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216327155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216327155">(Nov 11 2020 at 10:12)</a>:</h4>
<p>This is how <code>rustc_driver</code> creates <code>config::Options</code>: <a href="https://github.com/rust-lang/rust/blob/38030ffb4e735b26260848b744c0910a5641e1db/compiler/rustc_driver/src/lib.rs#L202-L207">https://github.com/rust-lang/rust/blob/38030ffb4e735b26260848b744c0910a5641e1db/compiler/rustc_driver/src/lib.rs#L202-L207</a></p>



<a name="216327307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216327307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216327307">(Nov 11 2020 at 10:14)</a>:</h4>
<p><code>handle_options</code> is exported from <code>rustc_driver</code>.</p>



<a name="216327367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216327367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216327367">(Nov 11 2020 at 10:15)</a>:</h4>
<p>aah, nice!</p>



<a name="216329214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216329214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216329214">(Nov 11 2020 at 10:36)</a>:</h4>
<p>Hmm, getting a <code>panicked at 'cannot access a scoped thread local variable without calling </code>set<code> first', scoped-tls-1.0.0/src/lib.rs:168:9</code> at backtrace<br>
   1: scoped_tls::ScopedKey&lt;T&gt;::with<br>
   2: rustc_session::parse::ParseSess::with_span_handler<br>
   3: rustc_session::session::build_session<br>
   4: rustc_interface::util::create_session</p>



<a name="216329975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216329975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216329975">(Nov 11 2020 at 10:45)</a>:</h4>
<p>You need to wrap it in <code>rustc_driver::catch_with_exit_code(|| { /* ... */ });</code>. You will probably want to check that the exit code is zero.</p>



<a name="216330023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216330023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216330023">(Nov 11 2020 at 10:45)</a>:</h4>
<p><code>catch_with_exit_code</code> spawns a new thread in which it initializes several thread locals that are used by various parts of rustc.</p>



<a name="216330173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216330173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216330173">(Nov 11 2020 at 10:47)</a>:</h4>
<p>Wait, it is <code>rustc_interface::interface::run_compiler</code> that does that.</p>



<a name="216330231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216330231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216330231">(Nov 11 2020 at 10:47)</a>:</h4>
<p><code>rustc_interface::interface::run_compiler</code> also gives you a <code>Session</code> for free.</p>



<a name="216330314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216330314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216330314">(Nov 11 2020 at 10:48)</a>:</h4>
<p>ah cool, thanks!</p>



<a name="216331018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Synthesizing%20a%20new%20Session%20for%20tests/near/216331018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Synthesizing.20a.20new.20Session.20for.20tests.html#216331018">(Nov 11 2020 at 10:56)</a>:</h4>
<p>neat, looks like that works, thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>